﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

using Stp.Core;

namespace Stp.Test
{
    [TestClass]
    public class CoreRegistryTest
    {
        CoreRegistry _cInst = CoreRegistry.GetInstance();
        List<string> _nList = new List<string>();
        XElement _plugins;

        [TestInitialize]
        public void Setup()
        {
            if (_nList.Count == 0)
            {
                using (FileStream fs = new FileStream("CtryNameE.txt", FileMode.Open, FileAccess.Read))
                {
                    StreamReader sr = new StreamReader(fs);
                    while (!sr.EndOfStream)
                    {
                        _nList.Add(sr.ReadLine());
                    }
                }
            }

            if (_plugins == null)
            {
                _plugins = XElement.Load("Plugins.xml");
            }
        }

        [TestMethod]
        public void CoreRegistry_CountryNameIntegrityTest()
        {
            Assert.AreEqual(_nList.Count, _cInst.CountryNameList.Count);
        }

        [TestMethod]
        public void CoreRegistry_CountryNameConsistencyTest()
        {
            foreach (string item in _nList)
            {
                Assert.IsTrue(_cInst.FindCountry(item));
            }
        }

        [TestMethod]
        public void CoreRegistry_CountryNameCaseSensitivityTest()
        {
            Assert.IsTrue(_cInst.FindCountry("New Zealand"));
        }

        [TestMethod]
        public void CoreRegistry_CurrentPathTest()
        {
            string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Assert.AreEqual(currentPath, _cInst.CurrentPath);
        }

        [TestMethod]
        public void CoreRegistry_GetDatalayerInfo()
        {
            XElement datalayerElement = _plugins.Element("Datalayer");
            Assert.AreEqual(datalayerElement.Element("Assembly").Value, _cInst.GetDatalayerInfo().AssemblyName);
            Assert.AreEqual(datalayerElement.Element("Namespace").Value, _cInst.GetDatalayerInfo().DefaultNamespace);
        }
    }
}
